# -*- coding: utf-8 -*-

from mtl.op_alloc import *

ADDR_MEM_START = 0
ADDR_SOFTMEM_START = {}


def malloc(size: int) -> int:
    global ADDR_MEM_START
    # Save the pointer.
    addr = ADDR_MEM_START
    # Increase the start.
    ADDR_MEM_START += size
    # Give back the addr.
    return addr


def musage() -> int:
    global ADDR_MEM_START
    return ADDR_MEM_START


def mplace(addr: int, data: list):
    # Assign the data.
    for offset, element in enumerate(data):
        dq(addr=addr + offset, data=element)


def smalloc(ticket: str, size: int) -> int:
    global ADDR_SOFTMEM_START
    # Check the used size.
    addr = ADDR_SOFTMEM_START[ticket] if ticket in ADDR_SOFTMEM_START else 0
    # Update the soft-mem usage.
    ADDR_SOFTMEM_START[ticket] = addr + size
    # Give back the list pointer.
    return addr


def smusage(ticket: str) -> int:
    global ADDR_SOFTMEM_START
    # Check the used size.
    return ADDR_SOFTMEM_START[ticket] if ticket in ADDR_SOFTMEM_START else 0
